
function editor()
{
	this.setup = function()
	{
		narzedzia =$(".draggable");

		for (i=0; i<narzedzia.length; i++)
		{

			el = narzedzia[i].firstChild;
			el.setAttribute('draggable', 'true');
			addEvent(el, 'dragstart', function (e) {
				e.dataTransfer.effectAllowed = 'copy'; // only dropEffect='copy' will be dropable
				e.dataTransfer.setData('Text', this.title); // required otherwise doesn't work
				
		    });

		}


	  bin = $("#canvas")[0];

	  addEvent(bin, 'drop', function (e) {
	    if (e.preventDefault) e.preventDefault(); // allows us to drop
	    this.className = 'over';
	    e.dataTransfer.dropEffect = 'copy';
	    //console.log('Drop');
				
		
		var el = e.dataTransfer.getData('Text')
		console.log(el);
		var slot = saveElement(this.clientX, this.clientY, el.toLowerCase(), true);
		
		if(slot) {
			engine.setValue("R" + slot, 5);
			engine.calculate();
		}
		
	    modelObject.canvasHandler.clearRect(this.clientX,this.clientY,100,100);
	    return false;
	  });


	  addEvent(bin, 'dragover', function (e) {
		
	    if (e.preventDefault) e.preventDefault(); // allows us to drop
	    this.className = 'over';
	    e.dataTransfer.dropEffect = 'copy';
	    var cX = Math.round((e.clientX-50)/100)*100;
	    var cY = Math.round((e.clientY-50)/100)*100;
	    


	    modelObject.canvasHandler.clearRect(this.clientX,this.clientY,100,100);
	    
	    //todo check if place is not empty
	    modelObject.canvasHandler.fillStyle   = '#50E01B'; // blue
	    
	     modelObject.canvasHandler.fillRect(cX,cY,100,100);
	     
	     
	    this.clientX = cX;
	    this.clientY = cY;
	    /*
	    
	    
	    
	    
	    */
	    return false;
	  });

	  addEvent(bin, 'dragenter', function (e) {
	    this.className = 'over';
	    console.log('drag!!!');
	    return false;
	  });

	  addEvent(bin, 'dragleave', function () {
	    this.className = '';
	  });



	}
}



$(function()
{
	edit = new editor();

	edit.setup(); 

});

var addEvent = (function () {
  if (document.addEventListener) {
    return function (el, type, fn) {
      if (el && el.nodeName || el === window) {
        el.addEventListener(type, fn, false);
      } else if (el && el.length) {
        for (var i = 0; i < el.length; i++) {
          addEvent(el[i], type, fn);
        }
      }
    };
  } else {
    return function (el, type, fn) {
      if (el && el.nodeName || el === window) {
        el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
      } else if (el && el.length) {
        for (var i = 0; i < el.length; i++) {
          addEvent(el[i], type, fn);
        }
      }
    };
  }
})();


/*
    setTimeout(function () {
      var t = setInterval(function () {
        if (y.style.opacity <= 0) {
          if (msie) { // don't bother with the animation
            y.style.display = 'none';
          }
          clearInterval(t); 
        } else {
          y.style.opacity -= 0.1;
        }
      }, 50);
    }, 250);

*/
  
